package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.alibaba.fastjson.JSON;
import com.sandking.db.cache.GlyCache;
import java.util.Map;
import com.sandking.io.SK_OutputStream;
import java.util.List;
import com.sandking.io.SK_InputStream;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * 管理员
 */
public class Gly {

	public static final String TABLENAME = "管理员";
	public static final String CLASSNAME = "Gly"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** 昵称 */
	private String nc;
	
	/** 账号 */
	private String zh;
	
	/** 密码 */
	private String mm;
	
	/** 绑定用户id */
	private int bdyhid;
	
	/** 绑定用户昵称 */
	private String bdyhnc;
	
	
	public Gly() {
		super();
	}
	
	public Gly(int id, String nc, String zh, String mm, int bdyhid, String bdyhnc) {
		super();
		this.id = id;
		this.nc = nc;
		this.zh = zh;
		this.mm = mm;
		this.bdyhid = bdyhid;
		this.bdyhnc = bdyhnc;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public String getNc() {
		return nc;
	}
	
	public void setNc(String nc) {
		this.nc = nc;
		addUpdateColumn("昵称",nc);
	}
	
	public String getZh() {
		return zh;
	}
	
	public void setZh(String zh) {
		this.zh = zh;
		addUpdateColumn("账号",zh);
	}
	
	public String getMm() {
		return mm;
	}
	
	public void setMm(String mm) {
		this.mm = mm;
		addUpdateColumn("密码",mm);
	}
	
	public int getBdyhid() {
		return bdyhid;
	}
	
	public void setBdyhid(int bdyhid) {
		this.bdyhid = bdyhid;
		addUpdateColumn("绑定用户id",bdyhid);
	}
	
	public void changeBdyhidWith(int bdyhid){
		this.bdyhid += bdyhid;
		setBdyhid(this.bdyhid);
	}
	
	public void changeBdyhidWithMin(int bdyhid,int min){
		this.bdyhid += bdyhid;
		this.bdyhid = this.bdyhid < min ? min : this.bdyhid;
		setBdyhid(this.bdyhid);
	}
	
	public void changeBdyhidWithMax(int bdyhid,int max){
		this.bdyhid += bdyhid;
		this.bdyhid = this.bdyhid > max ? max : this.bdyhid;
		setBdyhid(this.bdyhid);
	}
	
	public void changeBdyhidWithMaxMin(int bdyhid,int max,int min){
		this.bdyhid += bdyhid;
		this.bdyhid = this.bdyhid < min ? min : this.bdyhid;
		this.bdyhid = this.bdyhid > max ? max : this.bdyhid;
		setBdyhid(this.bdyhid);
	}	
	public String getBdyhnc() {
		return bdyhnc;
	}
	
	public void setBdyhnc(String bdyhnc) {
		this.bdyhnc = bdyhnc;
		addUpdateColumn("绑定用户昵称",bdyhnc);
	}
	
	
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("nc", this.nc);
        result.put("zh", this.zh);
        result.put("mm", this.mm);
        result.put("bdyhid", this.bdyhid);
        result.put("bdyhnc", this.bdyhnc);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("昵称", this.nc);
        result.put("账号", this.zh);
        result.put("密码", this.mm);
        result.put("绑定用户id", this.bdyhid);
        result.put("绑定用户昵称", this.bdyhnc);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeString(out,this.nc);
		    SK_OutputStream.writeString(out,this.zh);
		    SK_OutputStream.writeString(out,this.mm);
		    SK_OutputStream.writeInt(out,this.bdyhid);
		    SK_OutputStream.writeString(out,this.bdyhnc);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Gly createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Gly gly = new Gly();
		    gly.id = SK_InputStream.readInt(in,null);
		    gly.nc = SK_InputStream.readString(in,null);
		    gly.zh = SK_InputStream.readString(in,null);
		    gly.mm = SK_InputStream.readString(in,null);
		    gly.bdyhid = SK_InputStream.readInt(in,null);
		    gly.bdyhnc = SK_InputStream.readString(in,null);
		    return gly;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Gly> createForColumnNameList(List<Map<String, Object>> list){
    	List<Gly> glys = new ArrayList<Gly>();
		for (Map<String, Object> map : list) {
			glys.add(createForColumnNameMap(map));
		}
		return glys;
    }
    
    /**
     * 根据map创建对象
     */
    public static Gly createForColumnNameMap(Map<String, Object> map){
    	Gly obj = new Gly();
	    obj.id = SK_Map.getInt("id", map);
	    obj.nc = SK_Map.getString("昵称", map);
	    obj.zh = SK_Map.getString("账号", map);
	    obj.mm = SK_Map.getString("密码", map);
	    obj.bdyhid = SK_Map.getInt("绑定用户id", map);
	    obj.bdyhnc = SK_Map.getString("绑定用户昵称", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Gly> createForList(List<Map<String, Object>> list){
    	List<Gly> glys = new ArrayList<Gly>();
		for (Map<String, Object> map : list) {
			glys.add(createForColumnNameMap(map));
		}
		return glys;
    }
    
    /**
     * 根据map创建对象
     */
    public static Gly createForMap(Map<String, Object> map){
    	Gly obj = new Gly();
	    obj.id = SK_Map.getInt("id", map);
	    obj.nc = SK_Map.getString("nc", map);
	    obj.zh = SK_Map.getString("zh", map);
	    obj.mm = SK_Map.getString("mm", map);
	    obj.bdyhid = SK_Map.getInt("bdyhid", map);
	    obj.bdyhnc = SK_Map.getString("bdyhnc", map);
        return obj;
    }
    
    public static Gly createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Gly> createForJson(List<String> jsons){
    	List<Gly> glys = new ArrayList<Gly>();
    	for(String json : jsons){
    		glys.add(createForJson(json));
    	}
    	return glys;
    }
    
    /** 延迟插入数据库 */
    public Gly insert(){
    	return GlyCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Gly update(){
    	return GlyCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return GlyCache.delete(this);
    }
    /** 即时插入数据库 */
    public Gly insertAndFlush(){
    	return GlyCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Gly updateAndFlush(){
    	return GlyCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return GlyCache.deleteAndFlush(this);
    }
}